﻿using Basic.FlowDAL;
using Basic.FlowRemoteModel.NodeFormData.Model;
using WeDonekRpc.Helper;

namespace Basic.FlowCollect.lmpl
{
    internal class NodeFormDataRelationCollect : INodeFormDataRelationCollect
    {
        private readonly IFlowFormDataRelationDAL _BasicDAL;

        public NodeFormDataRelationCollect ( IFlowFormDataRelationDAL basicDAL )
        {
            this._BasicDAL = basicDAL;
        }
        public DataRelation[] Gets ( long nodeId )
        {
            return this._BasicDAL.Gets<DataRelation>(a => a.NodeId == nodeId);
        }
        public Result[] GetsByFlowId<Result> ( long flowId ) where Result : class, new()
        {
            return this._BasicDAL.Gets<Result>(a => a.FlowId == flowId);
        }
        public void Sync ( long flowId, long nodeId, DataRelation[] adds )
        {
            if ( this._BasicDAL.IsExists(a => a.NodeId == nodeId) )
            {
                this._BasicDAL.Set(flowId, nodeId, adds);
            }
            else
            {
                this._BasicDAL.Add(flowId, nodeId, adds);
            }
        }
        public void Clear ( long flowId )
        {
            long[] ids = this._BasicDAL.Gets(a => a.FlowId == flowId, a => a.Id);
            if ( ids.IsNull() )
            {
                return;
            }
            this._BasicDAL.Delete(ids);
        }
        public void ClearByNodeId ( long nodeId )
        {
            long[] ids = this._BasicDAL.Gets(a => a.NodeId == nodeId, a => a.Id);
            if ( ids.IsNull() )
            {
                return;
            }
            this._BasicDAL.Delete(ids);
        }
    }
}
